package com.luhuiguo.contacts.client.ui;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.Widget;

public class Info extends PopupPanel {

    private static InfoUiBinder uiBinder = GWT.create(InfoUiBinder.class);

    private static Info INSTANCE = new Info();

    interface InfoUiBinder extends UiBinder<Widget, Info> {
    }

    protected int duration = 2000;

    @UiField
    HTML msg;

    public Info() {
        this("");
    }

    public Info(String message) {
        setWidget(uiBinder.createAndBindUi(this));
        msg.setText(message);

    }

    @Override
    public void hide() {
        super.hide();
        afterHide();
    }

    public void show(String text, int duration) {
        this.duration = duration;
        onShowInfo(text);
    }

    public void show(String text) {
        onShowInfo(text);
    }
    
    protected void onShowInfo(String text) {
        msg.setText(text);
        center();
        //show();
        afterShow();
    }

    private void afterHide() {

    }

    private void afterShow() {
        Timer t = new Timer() {
            public void run() {
                msg.setText("");
                hide();
            }
        };
        t.schedule(duration);
    }

    public static void display(String text) {
        INSTANCE.show(text);
    }

    public static void display(String text, int duration) {
        INSTANCE.show(text, duration);
    }

}
